﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ZF.WS.UserControls;
using GCELibrary.Current;
using System.Transactions;
using ZF.BLL;
using Newtonsoft.Json;
using ZF.BLL.Admin;
using ZF.WS.Admin.service;
using ZF.BLL.ServiceModel;
namespace ZF.WS.Admin
{

    public partial class fsywManage : fsyw
    {
        [PageArgument(Key = "id", CanNull = false)]
        public int  Id { get; set; }
        public Model.Sys_Service Service { get; set; }
        public FSYW Model { get; set; }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Edit();
            }
        }
        public void InitControls()
        {

        }
        public void Edit()
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var service = dc.Sys_Service.FirstOrDefault(p => p.ServiceId == Id);
                if (service == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                InitControls();
                
                this.Service = service;
                this.Model = (FSYW)GetJsonObj(service.Json);
                this.tbInter.Text = this.Model.Intre.ToString();
            }
        }
        public object GetJsonObj(string json)
        {
            FSYW result = null;
            try
            {
                result = Newtonsoft.Json.JsonConvert.DeserializeObject<FSYW>(json);
            }
            catch (Exception ex)
            {
                result = new FSYW();
            }
            return result;
        }

        public void EditSubmit(object sender, EventArgs e)
        {
            using (var dc = BLL.SiteInfo.CreateDBDataContext())
            {
                var service = dc.Sys_Service.FirstOrDefault(p => p.ServiceId == Id);
                if (service == null)
                    throw WarnFactory.CreateWarn(WarnCode.ReocrdNoFind);
                service.IsAcc = true;

                this.Model = (FSYW)GetJsonObj(service.Json);
                this.Model.Intre = this.tbInter.Text.Convert<int>();
                service.Json = Newtonsoft.Json.JsonConvert.SerializeObject(this.Model);

                dc.SubmitChanges();
                WarnFactory.CreateWarn(new PageWarnParameter()
                {
                    JscriptMode = true,
                    Message = "确认成功",
                    Link = WarnLink.Current,
                }).Alert();
            }
        }
    }
 
}
